# copied from http://www.cmake.org/Wiki/CMakeMacroListOperations#LIST_CONTAINS
MACRO(LIST_CONTAINS var value)
	SET(${var})
	FOREACH(value2 ${ARGN})
		IF(${value} STREQUAL ${value2})
			SET(${var} TRUE)
		ENDIF()
	ENDFOREACH()
ENDMACRO()

# copied from http://www.cmake.org/Wiki/CMakeMacroParseArguments
MACRO(PARSE_ARGUMENTS prefix arg_names option_names)
	SET(DEFAULT_ARGS)
	FOREACH(arg_name ${arg_names})
		SET(${prefix}_${arg_name})
	ENDFOREACH()
	FOREACH(option ${option_names})
		SET(${prefix}_${option} FALSE)
	ENDFOREACH()

	SET(current_arg_name DEFAULT_ARGS)
	SET(current_arg_list)
	FOREACH(arg ${ARGN})
		LIST_CONTAINS(is_arg_name ${arg} ${arg_names})
		IF(is_arg_name)
			SET(${prefix}_${current_arg_name} ${current_arg_list})
			SET(current_arg_name ${arg})
			SET(current_arg_list)
		ELSE()
			LIST_CONTAINS(is_option ${arg} ${option_names})
			IF (is_option)
				SET(${prefix}_${arg} TRUE)
			ELSE()
				SET(current_arg_list ${current_arg_list} ${arg})
			ENDIF()
		ENDIF()
	ENDFOREACH()
	SET(${prefix}_${current_arg_name} ${current_arg_list})
ENDMACRO()

MACRO(GLOB_PATHS var)
	SET(GP_result)
	PARSE_ARGUMENTS(GP
		"GLOBS;BASES"
		"NOBASERESULT"
		${ARGN}
	)
	FOREACH(name ${GP_BASES})
		FOREACH(glob ${GP_GLOBS})
			SET(GP_globres)
			FILE(GLOB GP_globres ${name}/${glob})
			IF(NOT GP_NOBASERESULT)
				SET(GP_result ${GP_result} ${name})
			ENDIF()
			SET(GP_result ${GP_result} ${GP_globres})
		ENDFOREACH()
	ENDFOREACH()
	IF(GP_result)
		LIST(SORT GP_result ${GP_result})
		LIST(REVERSE GP_result ${GP_result})
	ENDIF()
	SET(${var} ${GP_result})
ENDMACRO()
